﻿using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.Web.Areas.CompletionMaterials.CompletionMaterialsFramework.Common;
using KingRoad.EPM.Web.Areas.CompletionMaterials.ViewModels;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KingRoad.EPM.Web.Areas.CompletionMaterials.Controllers
{
    public class FileDataController : BaseController
    {
        /// <summary>
        /// 竣工资料—归档材料类型表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            BcdFileDataViewModel vm = new BcdFileDataViewModel();
            vm.TheBcdFiledataInfo = new BcdFiledataInfo();
            return View(vm);
        }

        /// <summary>
        /// 归档材料Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetFileDataJson()
        {
            List<BcdFiledataInfo> typeList = BcdFiledataUIRule.GetEntityListBySql("");
            List<treeNodes> treenodes = new List<treeNodes>();
            foreach (BcdFiledataInfo info in typeList)
            {
                treeNodes node = new treeNodes();
                node.id = info.Fd_ID;
                node.pId = info.Fd_PID;
                if (info.Fd_PID == null)
                    node.open = "true";
                node.name = info.Fd_Numb + " " + info.Fd_Name;
                treenodes.Add(node);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 标段信息弹出框
        /// </summary>
        /// <param name="type">考核类型</param>
        /// <param name="ucodes">单位合同号集合</param>
        /// <returns></returns>
        public ActionResult SectionUnit(string action)
        {
            return View();
        }

        /// <summary>
        /// 归档材料表添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create(string id)
        {
            BcdFiledataInfo info = new BcdFiledataInfo();
            if (id != "0")
                info = BcdFiledataUIRule.GetEntityById(new Guid(id));
            BcdFileDataViewModel vm = new BcdFileDataViewModel();
            vm.TheBcdFiledataInfo = new BcdFiledataInfo();

            vm.TheBcdFiledataInfo.Fd_ID = Guid.NewGuid().ToString().ToUpper();
            vm.TheBcdFiledataInfo.Fd_PID = info.Fd_ID;
            vm.TheBcdFiledataInfo.Fd_PName = info.Fd_Name;
            vm.TheBcdFiledataInfo.Fd_Code = info.Fd_Code;
            vm.TheBcdFiledataInfo.Fd_Kit = info.Fd_Kit;
            vm.TheBcdFiledataInfo.Fd_Kept = info.Fd_Kept;


            if (id != "0")
            {
                vm.TheBcdFiledataInfo.Fd_Order = BcdFiledataUIRule.GetCountByFId(id) + 1;
                vm.TheBcdFiledataInfo.Fd_UType = info.Fd_UType;
            }
            else
            {
                vm.TheBcdFiledataInfo.Fd_UType = "项目法人";
                vm.TheBcdFiledataInfo.Fd_Order = BcdFiledataUIRule.GetCountByFId(null) + 1;
            }


            vm.TheBcdFiledataInfo.Fd_PIds = info.Fd_PIds + ",'" + info.Fd_ID + "'";

            List<SelectListItem> utypelist = new List<SelectListItem>();
            utypelist.Add(new SelectListItem { Text = "项目法人", Value = "项目法人" });
            utypelist.Add(new SelectListItem { Text = "设计单位", Value = "设计单位" });
            utypelist.Add(new SelectListItem { Text = "施工单位", Value = "施工单位" });
            utypelist.Add(new SelectListItem { Text = "监理单位", Value = "监理单位" });
            utypelist.Add(new SelectListItem { Text = "施工、监理", Value = "施工、监理" });
            utypelist.Add(new SelectListItem { Text = "相关单位", Value = "相关单位" });
            ViewBag.utypelist = utypelist;

            List<SelectListItem> keptlist = new List<SelectListItem>();
            keptlist.Add(new SelectListItem { Text = "永久", Value = "永久" });
            keptlist.Add(new SelectListItem { Text = "长期", Value = "长期" });
            keptlist.Add(new SelectListItem { Text = "短期", Value = "短期" });
            keptlist.Add(new SelectListItem { Text = "30年", Value = "30年" });
            ViewBag.keptlist = keptlist;
            return View(vm);
        }

        /// <summary>
        /// 添加页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreatePost(BcdFileDataViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdFiledataInfo.Fd_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdFiledataInfo.Fd_OperateUserName = GetLoginUserRealName;
                vm.TheBcdFiledataInfo.Fd_OperateTime = DateTime.Now;
                
                BcdFiledataUIRule.InsertEntity(vm.TheBcdFiledataInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "add",
                    pId = vm.TheBcdFiledataInfo.Fd_PID,
                    id = vm.TheBcdFiledataInfo.Fd_ID,
                    name = vm.TheBcdFiledataInfo.Fd_Numb + " " + vm.TheBcdFiledataInfo.Fd_Name,
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 归档材料表修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            BcdFileDataViewModel vm = new BcdFileDataViewModel();
            vm.TheBcdFiledataInfo = BcdFiledataUIRule.GetEntityById(new Guid(id));

            List<SelectListItem> utypelist = new List<SelectListItem>();
            utypelist.Add(new SelectListItem { Text = "项目法人", Value = "项目法人" });
            utypelist.Add(new SelectListItem { Text = "设计单位", Value = "设计单位" });
            utypelist.Add(new SelectListItem { Text = "施工单位", Value = "施工单位" });
            utypelist.Add(new SelectListItem { Text = "监理单位", Value = "监理单位" });
            utypelist.Add(new SelectListItem { Text = "施工、监理", Value = "施工、监理" });
            utypelist.Add(new SelectListItem { Text = "相关单位", Value = "相关单位" });
            ViewBag.utypelist = utypelist;

            List<SelectListItem> keptlist = new List<SelectListItem>();
            keptlist.Add(new SelectListItem { Text = "永久", Value = "永久" });
            keptlist.Add(new SelectListItem { Text = "长期", Value = "长期" });
            keptlist.Add(new SelectListItem { Text = "短期", Value = "短期" });
            keptlist.Add(new SelectListItem { Text = "30年", Value = "30年" });
            ViewBag.keptlist = keptlist;

            return View(vm);
        }

        /// <summary>
        /// 修改页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditPost(BcdFileDataViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdFiledataInfo.Fd_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdFiledataInfo.Fd_OperateUserName = GetLoginUserRealName;
                vm.TheBcdFiledataInfo.Fd_OperateTime = DateTime.Now;
                BcdFiledataUIRule.UpdateEntity(vm.TheBcdFiledataInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    type = "edit",
                    pId = vm.TheBcdFiledataInfo.Fd_PID,
                    id = vm.TheBcdFiledataInfo.Fd_ID,
                    name = vm.TheBcdFiledataInfo.Fd_Numb + " " + vm.TheBcdFiledataInfo.Fd_Name,
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdFiledataUIRule.DeleteEntity(id);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    type = "delete",
                    pId = "",
                    id = id,
                    name = "",
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(string id)
        {
            BcdFiledataInfo info = new BcdFiledataInfo();
            if (id != "0")
                info = BcdFiledataUIRule.GetEntityById(new Guid(id));
            return View(info);
        }
        /// <summary>
        /// 更新父子集合ID
        /// </summary>
        /// <returns></returns>
        public JsonResult SonAndParentIds()
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //更新父类型集合
                BcdFiledataUIRule.UpdateParentIds(null);
                ////更新子类型集合(集合长度过长无法使用)
                //BcdFiledataUIRule.UpdateSonIds(null);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "更新成功",
                    type = "ids",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "更新失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
    }
}
